<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Route
 *
 * @author valentin
 */
class RM_JoomlaSEF_Router_Route_Admin extends RM_Joomla_Controller_Router_Route
{
    /**
     * @var string
     */
    protected $_alias = 'resmania';

    /**
     * Prepares the route for mapping.
     *
     * @param string $route Map used to match with later submitted URL path
     * @param array $defaults Defaults for map variables with keys as variable names
     */
    public function __construct($defaults = array())
    {
        // get the alias name
        $joomlaSEF = new RM_JoomlaSEF;
        $settings = $joomlaSEF->getSettings()->toArray();
        $this->_alias = $settings[0]['aliasname'];

        $this->_controller = $defaults['controller'];
        $this->_action = $defaults['action'];
        $this->_module = $defaults['module'];
    }

    /**
     * Returns an js code of function that assembles a URL path
     *
     * @todo typo in method name is terrible...
     * @todo see _getAssembledURLJsCode16 method todo, after that will be done we could remove this method as well
     * 'private' and after this will be able to remove this method from here
     * @return string JS function named 'RM.Common.AssembleURL' for assemble
     */
    public function getAccembleURLJsCode()
    {
        if (RM_Joomla_Connector::is16()) {
            return $this->_getAssembledURLJsCode16();
        }
        if (RM_Joomla_Connector::is15()) {
            return $this->_getAssembledURLJsCode15();
        }
        return null;
    }

    /**
     * @todo it's just a copy of method RM_Joomla_Controller_Router_Route we need to make it
     * 'protected' there instead of
     * 'private' and after this will be able to remove this method from here
     * @return string
     */
    private function _getAssembledURLJsCode16()
    {
        return "RM.Common.AssembleURL = function(json){
            var url = 'index.php?option=com_resmania&act='+json.controller+'&task='+json.action+'&format=row';
            if (json.parameters) {
                for (var i = 0; i < json.parameters.length; i++){
                    url = url + '&' + json.parameters[i].name + '=' + json.parameters[i].value;
                }
            }
            return url;
        }";
    }

    private function _getAssembledURLJsCode15()
    {
        return "RM.Common.AssembleURL = function(json){
            if (json.forcefrontend === true){
                var url = '".$this->_alias."/'+json.controller+'/'+json.action;
            } else {
                var url = 'index3.php?option=com_resmania&act='+json.controller+'&task='+json.action;
                if (json.parameters) {
                    for (var i = 0; i < json.parameters.length; i++){
                        url = url + '&' + json.parameters[i].name + '=' + json.parameters[i].value;
                    }
                }
            }
            return url;
        }";
    }
}